package org.mappland.compltres.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.mappland.compltres.common.CommonResult;
import org.mappland.compltres.common.StatusCode;
import org.mappland.compltres.domain.Machine;
import org.mappland.compltres.domain.Order;
import org.mappland.compltres.dto.AdminReturnMachineDTO;
import org.mappland.compltres.service.MachineService;
import org.mappland.compltres.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("adminMachineController")
@RequestMapping("/admin")
public class MachineController {
    @Resource
    private MachineService machineService;
    @Resource
    private OrderService orderService;

    // 管理员获取机器列表
    @GetMapping("/listMachines")
    public CommonResult<List<Machine>> listMachien() {
        CommonResult<List<Machine>> result = new CommonResult<>();
        List<Machine> machineList = machineService.list();

        result.setCode(StatusCode.COMMON_SUCCESS.getCode());
        result.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        result.setData(machineList);

        return result;
    }

    // 管理员获取机器详情
    @PostMapping(value = "/machineDetail")
    public CommonResult<AdminReturnMachineDTO> machineDetail(@RequestParam("machineId") Integer machineId) {
        CommonResult<AdminReturnMachineDTO> commonResult = new CommonResult<>();

        AdminReturnMachineDTO returnMachineDTO = machineService.adminReturnMachine(machineId);

        commonResult.setData(returnMachineDTO);
        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        return commonResult;
    }

    // 管理员新增加机器
    @PostMapping("/addMachine")
    public CommonResult<String> addMachine(@RequestBody Machine machine) {
        CommonResult<String> commonResult = new CommonResult<>();
        Machine machineByNumber = machineService.getOne(new QueryWrapper<Machine>().eq("ip", machine.getIp()));
        if(machineByNumber != null) {
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("机器ip已存在!");
            return commonResult;
        }
        boolean result = machineService.save(machine);

        if (result) {
            commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
            commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
            commonResult.setData("添加机器成功!");
        } else {
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("添加机器失败!");
        }

        return commonResult;
    }

    @PostMapping("/updateMachine")
    public CommonResult<String> updateRoom(@RequestBody Machine machine) {
        CommonResult<String> commonResult = new CommonResult<>();
        Machine machineByNumber = machineService.getOne(new QueryWrapper<Machine>().eq("ip", machine.getIp()).ne("id", machine.getId()));
        if(machineByNumber != null) {
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("ip已存在!");
            return commonResult;
        }

        boolean result = machineService.updateById(machine);

        if (result) {
            commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
            commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
            commonResult.setData("修改机器信息成功");
        } else {
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("修改机器信息失败");
        }

        return commonResult;
    }

    // 管理员删除机器
    @PostMapping("/deleteMachine")
    public CommonResult<String> deleteRoom(@RequestParam("machineId") Integer machineId) {
        CommonResult<String> commonResult = new CommonResult<>();


        // 移除关联订单
        orderService.remove(
                new QueryWrapper<Order>().eq("machine_id", machineId)
        );

        boolean result = machineService.removeById(machineId);

        if (result) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("machine_id", machineId);
            orderService.remove(queryWrapper);

            commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
            commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
            commonResult.setData("删除机器成功!");
        } else {
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("删除机器失败!");
        }

        return commonResult;
    }
}
